<template>
  <div>
    <div class="pd-10">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
    </div>

    <div class="mg-10">
      <el-button type="primary" @click="handleAdd">新增<i class="el-icon-circle-plus-outline ml-5"></i></el-button>
      <el-popconfirm
          confirm-button-text='是的我确定'
          cancel-button-text='算了在想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定删除吗？"
          @confirm="delBatch"
          class="ml-5"
      >
        <el-button type="danger" slot="reference">批量删除<i class="el-icon-remove-outline ml-5"></i></el-button>
      </el-popconfirm>
      </div>

    <el-table
        :data="tableData" border stripe
        :header-cell-style="{ background:'#eee',border: '1px solid #ccc' ,color: '#000'}"
        @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="id" label="id" width="100px">
      </el-table-column>
      <el-table-column prop="name" label="商品名称" width="300">
      </el-table-column>
      <el-table-column prop="type" label="商品类别" width="100">
      </el-table-column>
      <el-table-column prop="goodsImgUrl" label="商品展示图片" width="300">
        <template slot-scope="scope">
          <el-empty description="标题图片" :image="scope.row.goodsImgUrl"></el-empty>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="商品价格" width="100">
      </el-table-column>
      <el-table-column prop="sales" label="总销售数量" width="100">
      </el-table-column>
      <el-table-column prop="isPutaway" label="是否上架" width="100">
        <template slot-scope="scope">
          <el-switch  v-model="scope.row.isPutaway == 1 " active-color="#13ce66" inactive-color="#ccc" @change="changeEnable(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">

        <template slot-scope="scope" >
          <el-button type="success" @click="handleEdit(scope.row)" >编辑<i class="el-icon-edit ml-5"></i></el-button>
          <el-popconfirm
              confirm-button-text='是的我确定'
              cancel-button-text='算了在想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="handledelete(scope.row.id)"
              class="ml-5"
          >
            <el-button type="danger" slot="reference">删除<i class="el-icon-delete ml-5"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0;">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="课程信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form"  label-width="80px" size="small">
        <el-form-item label="商品名称" >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="商品类别" >
          <el-select clearable v-model="form.type">
            <el-option v-for="item in goodsTyps " :key="item.id" :label="item.name" :value="item.name"> </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品展示图片" >
          <el-upload
              :action=" serverIp +'/files/upload'"
              :on-success="handleAvatarSuccess"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="form.goodsImgUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="销售价格" >
          <el-input-number v-model="form.price" :precision="2" :step="0.1" :max="200000"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>

import {serverIp} from "../../../public/config";

export default {
  name: "Goods",
  data(){
    return{
      serverIp: serverIp,
      dialogVisible: false,
      tableData: [], //分页数据
      total: 0, //总条数
      pageNum: 0,
      pageSize: 5,
      form: {}, //表格数据
      name: "", //查询
      multipleSelection: [], //多选删除
      dialogFormVisible: false, //新增按钮
      goodsTyps: [{
          id: 1,
          name: "图书"
        },
        {
          id: 2,
          name: "科技"
        }], //商品类别

    }
  },
  created() {
    this.load();
  },
  methods:{
    //分页请求
    load(){
      this.request.get("/goods/page",{
        params:{
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
        }
      }).then(res =>{
        if(res.code === "200"){
          this.tableData = res.data.data
          this.total = res.data.total
        }else {
          this.$message.error(res.msg)
        }


      })
    },
    //分页方法
    handleSizeChange(pageSize) {
      this.pageSize=pageSize;
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load()
    },

    //图片上传方法
    handleAvatarSuccess(res, file) {
      console.log(URL.createObjectURL(file.raw))
      // this.imageUrl = URL.createObjectURL(file.raw);
      this.form.goodsImgUrl = res.data;
    },
    //重置搜索框
    reset(){
      this.name = "";
      this.load()
    },
    //新增按钮
    handleAdd(){
      this.dialogFormVisible = true;
      this.form = {}
    },
    //确定新增
    save(){
      this.request.post("/goods",
          this.form).then(res=>{
        if(res.code ==="200"){
          this.$message.success(res.data)
          this.form ={}
          this.dialogFormVisible = false;
          this.load()
        }else {
          this.$message.error(res.msg)
        }
      })
    },
    //编辑按钮
    handleEdit(row){
      this.form = row
      this.dialogFormVisible = true
    },

    //取消按钮
    cancel() {
      this.dialogFormVisible=false
      this.load()
    },
    //删除按钮
    handledelete(id){
      this.request.delete("/goods/"+id).then(res=>{
        if(res.code === "200"){
          this.$message.success(res.data)
          this.load()
        }else {
          this.$message.error(res.msg)
        }
      })
    },
    //批量删除多选
    handleSelectionChange(val){
      this.multipleSelection  = val
    },
    //点击批量删除
    delBatch(){
      let ids= this.multipleSelection.map(v => v.id)  //将数组内的id提取出来
      this.request.delete("/goods/del/batch",{
        data: ids
      }).then(res=>{
        if(res.code === "200"){
          this.$message.success(res.data)
          this.load()
        }else {
          this.$message.error(res.msg)
        }
      })
    },
    //在线预览
    kkfileview(url){
      window.open('https://file.keking.cn/onlinePreview?url=' + encodeURIComponent(window.btoa((url))))
    },
    //导入成功的回调
    handleFileUploadSuccess(res){
      this.load();
      this.$message.success("上传成功")
    },
    //下载连接
    download(url){
      window.open(url);
    },
    changeEnable(row){
      row.isPutaway = row.isPutaway == 1? 0: 1;
      this.request.post("/goods",row).then(res=>{
          if (res.code === "200"){
            this.$message.success("操作成功")
          } else {
            this.$message.error("操作失败")
          }
      })
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      // this.form.goodsImgUrl = file.url;
      this.dialogVisible = true;
    }

  }
}
</script>

<style scoped>

</style>